<template>
  <div style="width: 500px">
    <el-input
            placeholder="请输入内容"
            prefix-icon="el-icon-search"
            size="small"
            v-model="filterText">
    </el-input>
    <el-tree
            class="filter-tree"
            :data="deps"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree">
      <span class="custom-tree-node"
            style="display: flex;justify-content: space-between;width: 100%"
            slot-scope="{ node, data }">
        <span>{{ data.name }}</span>
        <span>
          <el-button
                  class="depBtn"
                  type="primary"
                  size="mini"
                  @click="() => showAddDepView(data)">
            添加部门
          </el-button>
          <el-button
                  class="depBtn"
                  type="danger"
                  size="mini"
                  @click="() => deleteDep(data)">
            删除部门
          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="30%">
      <div>
        <table>
          <tr>
            <td>
              <el-tag>上级部门</el-tag>
            </td>
            <td>{{pname}}</td>
          </tr>
          <tr>
            <td>
              <el-tag>部门名称</el-tag>
            </td>
            <td>
              <el-input v-model="dep.name" placeholder="请输入部门名称..."></el-input>
            </td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="doAddDep">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "DepMana",
    data() {
      return {
        //上级部门
        pname: '',
        // 添加数据
        dep: {
          name: '',
          parentId: -1
        },
        dialogVisible: false,
        filterText: '',
        deps: [],
        defaultProps: {
          children: 'children',
          label: 'name'
        }
      }
    },
    mounted() {
      this.initDeps()
    },
    watch: {
      // 监听 filterText 属性，注意属性名一定要一致
      filterText(val) {
        this.$refs.tree.filter(val);
      }
    },
    methods: {
      // 根据删除的部门数据，更新 deps 数组
      removeDepFromDeps(p,deps,id){
        for (let i=0; i <  deps.length; i++){
          let d = deps[i];
          if (d.id == id){
            deps.splice(i,1);
            if (deps.length == 0){
              p.parent = false;
            }
            return;
          }else {
            this.removeDepFromDeps(d,d.children,id);
          }
        }
      },
      // 初始化数据
      initDep(){
        this.dep = {
          name: '',
          parentId: -1
        }
      },
      // 将添加成功后的部门记录添加到 deps 数组中
      addDep2Deps(deps, dep) {
        for (let i = 0; i < deps.length; i++) {
          let d = deps[i]
          if (d.id == dep.parentId) {
            // 如果当前数据为新添加部门的父部门，则将数据追加到子节点中。concat 为追加函数
            d.children = d.children.concat(dep);
            // 如果子节点长度大于 0 ，则表示该节点为父节点
            if (d.children.length > 0) {
              d.parent = true;
            }
          }else {
            // 递归
            this.addDep2Deps(d.children,dep);
          }
        }
      },
      // 添加部门
      doAddDep() {
        this.postRequest("/system/basic/department/", this.dep).then(resp => {
          if (resp) {
            this.addDep2Deps(this.deps,resp.obj);
            this.dialogVisible = false;
            this.initDep();
          }
        });
      },
      // 删除部门
      deleteDep(data) {
        this.$confirm('此操作将永久删除【'+ data.name+' 】部门, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest("/system/basic/department/" + data.id).then(resp => {
            if (resp) {
              this.removeDepFromDeps(null,this.deps,data.id);
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 展示添加部门视图
      showAddDepView(data) {
        this.pname = data.name;
        this.dep.parentId = data.id;
        this.dialogVisible = true;
      },
      //初始化，获取所有部门信息
      initDeps() {
        this.getRequest("/system/basic/department/").then(resp => {
          if (resp) {
            this.deps = resp;
          }
        })
      },
      // 查询树
      filterNode(value, data) {
        if (!value) return true;
        return data.name.indexOf(value) !== -1;
      }
    }
  }
</script>

<style>
  .depBtn {
    padding: 2px;
  }

</style>